﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ApplicationTemplate
{
    class CreateModelClass
    {

        public static String CreateModelFields(DataSet ds)
        {
            String fields = String.Empty;
            int index = 0;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                String name = row["name"].ToString();
                String user_type_id = row["user_type_id"].ToString();

                String type = "String";
                String value = " = String.Empty";
                String pk = "";

                //对应58 smalldatetime
                if (user_type_id == "61" || user_type_id == "40" || user_type_id == "58")
                {
                    type = "DateTime?";
                    value = " = null";
                }

                if (user_type_id == "189")
                {
                    type = "TimeSpan";
                    value = "";
                }
                //Add by wangsw begin 20121018 beign
                if (user_type_id == "52")
                {
                    type = "Int16";
                }
                if (user_type_id == "56")
                {
                    type = "Int32";
                }
                if (user_type_id == "127")
                {
                    type = "Int64";
                }
                //Add by wangsw begin 20121018 end

                //Add by changqiguo 20121029
                if (user_type_id == "106")
                {
                    type = "Decimal";
                }
                if (user_type_id == "104")
                {
                    type = "Int32";
                }

                if (user_type_id == "127" || user_type_id == "104" || user_type_id == "56" || user_type_id == "52"||user_type_id=="106")
                {
                    value = " = 0";
                }

                if (row["is_pk"].ToString() == "1")
                {
                    pk = "PK:";
                }

                if (index == 0)
                {
                    fields += String.Format(
                        @"/// <summary>
        /// {3}{1}
        /// </summary>
        public {0} {1}{2};
", type, name, value, pk);
                }
                else
                {
                    fields += String.Format(
                        @"
        /// <summary>
        /// {3}{1}
        /// </summary>
        public {0} {1}{2};
", type, name, value, pk);
                }

                index++;
            }
            return fields;
        }

        public static String CreateModelTemplate(String spaceName, String className, String fields)
        {
            String model = String.Format(@"using System;

namespace {0}
{{
    [Serializable]
    public partial class {1}
    {{
        {2}
    }}
}}", spaceName, className, fields);


            return model;
        }
    }
}
